package fr.inserm.umr915.vcfserver.bio;

import java.util.Comparator;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;

/**
 * 
 * Identifier
 *
 */
public class Identifier
	implements Comparable<Identifier>
	{
	private String name;
	private short dbId;
	
	public Identifier(String name,short dbId)
		{
		this.name=name;
		this.dbId=dbId;
		}
	
	public String getName()
		{
		return name;
		}
	
	public short getDatabase()
		{
		return dbId;
		}

	@Override
	public int hashCode()
		{
		final int prime = 31;
		int result = 1;
		result = prime * result + dbId;
		result = prime * result + name.hashCode();
		return result;
		}

	@Override
	public boolean equals(Object obj)
		{
		if (this == obj) { return true; }
		if (obj == null) { return false; }
		if (getClass() != obj.getClass()) { return false; }
		Identifier other = (Identifier) obj;
		if (dbId != other.dbId) { return false; }
		if (!name.equals(other.name)) { return false; }
		return true;
		}
	
	@Override
	public String toString()
		{
		return getName()+"("+getDatabase()+")";
		}
	
	@Override
	public int compareTo(Identifier o)
		{
		int i= this.getName().compareToIgnoreCase(o.getName());
		if(i!=0) return i;
		return getDatabase()-o.getDatabase();
		}
	
	public static class Binding extends TupleBinding<Identifier>
		{
		public Binding()
			{	
			}
		
		@Override
		public Identifier entryToObject(TupleInput in)
			{
			String c=in.readString();
			short i=in.readShort();
			return new Identifier(c,i);
			}
		
		@Override
		public void objectToEntry(Identifier o, TupleOutput out)
			{
			out.writeString(o.getName());
			out.writeShort(o.getDatabase());
			}
		}
	
	
	public static class Sorter implements Comparator<byte[]>
		{
		public Sorter()
			{
			}
		@Override
		public int compare(byte[] o1, byte[] o2)
			{
			TupleInput in1=new TupleInput(o1);
			TupleInput in2=new TupleInput(o2);
			
			String s1=in1.readString();
			String s2=in2.readString();
			int i=s1.compareToIgnoreCase(s2);
			if(i!=0) return i;
			
			short d1= in1.readShort();
			short d2= in2.readShort();
			
			return d1-d2;
			}
		}
	}
